package cn.HL.org.controller;

import cn.HL.basic.anno.PreAuthorize;
import cn.HL.basic.util.AjaxResult;
import cn.HL.basic.util.PageList;
import cn.HL.org.domain.Employee;
import cn.HL.org.query.EmployeeQuery;
import cn.HL.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/employee")
@RestController
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询一条
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询" )

    @GetMapping("/{id}")
    public Employee findEmployeeById(@PathVariable("id")     @ApiParam(name="ID属性唯一主键",required = true) Long id){
        return employeeService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有哦" )
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }
    /**
     * 高级查询
     */
    @ApiOperation(value = "高级查询和分页" )
    @PostMapping
    public PageList<Employee> findAll(@RequestBody @ApiParam(name="高级查询条件和分页条数",required = true) EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }
    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )

    public AjaxResult removeById(@PathVariable("id") @ApiParam(name="ID属性唯一主键",required = true)  Long id){
        try {
            employeeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMessage("系统有问题哦 ").setSuccess(false);
        }
    }
    @PutMapping
    @ApiOperation(value = "新增和修改" )
    public AjaxResult updateOrsavae(@RequestBody @ApiParam(name="Employee对象数据",required = true) Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return   AjaxResult.me().setMessage("系统有问题哦 ").setSuccess(false);
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchDelDept(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelDept(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMessage("系统有问题哦 ").setSuccess(false);
        }
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> findByShop(@PathVariable("shopId")Long shopId){

        return employeeService.findByShop(shopId);
    }


}
